package org.dromara.third.convert.spaceWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.SpaceWork;
import org.dromara.system.mapper.SpaceWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.spaceWork.dto.SpaceWorkDTO;
import org.dromara.third.convert.spaceWork.validator.SpaceWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertSpaceWorkController {

    @Autowired
    private SpaceWorkMapper spaceWorkMapper;

    @PostMapping("/spaceWork")
    public R<Void> addSpaceWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        SpaceWorkDTO spaceWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            spaceWorkDTO = JSONObject.parseObject(decrypt, SpaceWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, spaceWorkDTO, new SpaceWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        SpaceWork spaceWork = assembleSpaceWorkData(orgCode, spaceWorkDTO);
        int count = spaceWorkMapper.countWithOutDelFlag(spaceWork.getId());
        if (count == 0) {
            spaceWorkMapper.insert(spaceWork);
        } else {
            spaceWorkMapper.updateWithOutDelFlag(spaceWork);
        }
        return R.ok();
    }

    private SpaceWork assembleSpaceWorkData(String orgCode, SpaceWorkDTO dto) {
        SpaceWork spaceWork = new SpaceWork();
        try {
            spaceWork.setId(dto.getZyid());
            spaceWork.setZyzbh(dto.getZyzbh());
            spaceWork.setSqdw(dto.getSqdw());
            spaceWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            spaceWork.setSqr(dto.getSqr());
            spaceWork.setZynr(dto.getZynr());
            spaceWork.setZywz(dto.getZywz());
            spaceWork.setYyjzmc(dto.getYyjzmc());
            spaceWork.setZyryxx(dto.getZyryxx());
            spaceWork.setZydwlx(dto.getZydwlx());
            spaceWork.setZydwmc(dto.getZydwmc());
            spaceWork.setZyfzr(dto.getZyfzr());
            spaceWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            spaceWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            spaceWork.setSfsjqttszy(dto.getSfsjqttszy());
            spaceWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            spaceWork.setFxbs(dto.getFxbs());
            spaceWork.setAqjdr(dto.getAqjdr());
            spaceWork.setJsjdr(dto.getJsjdr());
            spaceWork.setJhr(dto.getJhr());
            spaceWork.setDqzt(dto.getDqzt());
            spaceWork.setLongitude(dto.getLongitude());
            spaceWork.setLatitude(dto.getLatitude());
            spaceWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            spaceWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            spaceWork.setSpgbbm(dto.getSpgbbm());
            spaceWork.setFhyp(dto.getFhyp());
            spaceWork.setAttachment(dto.getAttachment());
            spaceWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            spaceWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            spaceWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            spaceWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            spaceWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            spaceWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            spaceWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装动火作业信息异常");
        }
        return spaceWork;
    }
}
